//
//  CollectionDetailViewController.m
//  LubanWork
//
//  Created by xiaohao on 2020/1/2.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "CollectionDetailViewController.h"
#import "CollectionDetailTableViewCell.h"
#import "MyCollectionModel.h"
#import "WorkDetailViewController.h"



@interface CollectionDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * table;
@property(nonatomic,strong) MyCollectionModel * colectionModel;

@property(nonatomic,strong) UIImageView * noDataImg;
@property(nonatomic,assign) NSInteger page;
@end

@implementation CollectionDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navString;
    
    self.page = 1;
    [self setTable];
    [self loadData];
    
}
-(void)noData{
    [self.view addSubview:self.noDataImg];
    [self.noDataImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(kSCRATIO(232));
        make.height.mas_offset(kSCRATIO(215));
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
    }];
}
-(UIImageView *)noDataImg{
    if (!_noDataImg) {
        _noDataImg = [[UIImageView alloc] init];
        _noDataImg.image = [UIImage imageNamed:@"Mine_baoming"];
    }
    return _noDataImg;
}
-( void)setTable{
    [self setExtraCellLineHidden:self.table];
    [self.table setSeparatorInset:UIEdgeInsetsZero];
    [self.view addSubview:self.table];
}
- (void)setExtraCellLineHidden:(UITableView *)tab{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tab setTableFooterView:view];
}
- (UITableView *)table{
    if (_table == nil) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, KSCHEIGHT(5), WIDTH, HEIGHT-KSCHEIGHT(10)-kNavHeight) style:UITableViewStylePlain];
        _table.delegate =self;
        _table.dataSource = self;
        _table.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
        }];
        _table.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self loadData];
        }];
        
    }
    return _table;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.colectionModel.result.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifierID = [NSString stringWithFormat:@"%ld,%ld",(long)[indexPath section],[indexPath row]];
    CollectionDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (!cell) {
        cell = [[CollectionDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierID];
    }
    MyCollectionModelResult *list = self.colectionModel.result[indexPath.row];
    cell.titLab.text = list.name;
    
    cell.dayLab.text = [list.wagetype isEqualToNumber:@1] == YES ? @"日结":@"月结";
    
    cell.detaiLab.text = list.jobsite;
    cell.timeLab.text = [list.jobtime isEqualToNumber:@1] == YES ? @"长期":@"短期";
    if ([list.sex isEqualToNumber:@1]) {
        cell.sexLab.text = @"男";
    }else if ([list.sex isEqualToNumber:@2]){
        cell.sexLab.text = @"女";
    }else{
        cell.sexLab.text = @"男女不限";
    }
    
    cell.priceLab.text = [NSString stringWithFormat:@"%d",[list.wagemax intValue]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionModelResult *list = self.colectionModel.result[indexPath.row];
    WorkDetailViewController *detail = [[WorkDetailViewController alloc] init];
    detail.detailID = list.Id;
    [self.navigationController pushViewController:detail animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KSCHEIGHT(90);
}
-(void)loadData{
    if (self.isNav) {
        NSDictionary *dic = @{
            @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.page],
            @"pageSize":@"10",
            @"name":self.name,
            @"choice":@"",
            @"jobcategory":@"",
            @"jobtype":@"",
            @"worktime":@"",
            @"jobsite":[AppDelegate shareAppDelegate].province
        };
        __weak typeof(self) weakSelf = self;
        [[NetWorkManager sharedManager]getData:@"api/jianzhi/getJianzhi" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([responseObject[@"code"]isEqualToNumber:@200]) {
                weakSelf.colectionModel = [MyCollectionModel yy_modelWithJSON:responseObject];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (weakSelf.colectionModel.result.count>0) {
                        [weakSelf setTable];
                    }else{
                        [weakSelf noData];
                    }
                    [weakSelf.table reloadData];
                    [weakSelf.table.mj_header endRefreshing];
                    [weakSelf.table.mj_footer endRefreshing];
                });
            }else{
                
            }
            //        UserModel *user = [UserModel sharedInstance:UserKey0];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HWProgressHUD showMessage:@"请联系管理员"];
                [weakSelf.table.mj_header endRefreshing];
                [weakSelf.table.mj_footer endRefreshing];
            });
            
        }];
        
    }else{
        NSDictionary *dic = @{
            @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.page],
            @"pageSize":@"10",
            @"name":@"",
            @"choice":@"",
            @"jobcategory":[NSString stringWithFormat:@"%ld",(long)self.indexForPage],
            @"":@"",
        };
        __weak typeof(self) weakSelf = self;
        [[NetWorkManager sharedManager]getData:@"api/jianzhi/getJianzhi" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([responseObject[@"code"]isEqualToNumber:@200]) {
                weakSelf.colectionModel = [MyCollectionModel yy_modelWithJSON:responseObject];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (weakSelf.colectionModel.result.count>0) {
                        [weakSelf setTable];
                    }else{
                        [weakSelf noData];
                    }
                    [weakSelf.table reloadData];
                    [weakSelf.table.mj_header endRefreshing];
                    [weakSelf.table.mj_footer endRefreshing];
                });
            }else{
                
            }
            //        UserModel *user = [UserModel sharedInstance:UserKey0];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HWProgressHUD showMessage:@"请联系管理员"];
                [weakSelf.table.mj_header endRefreshing];
                [weakSelf.table.mj_footer endRefreshing];
            });
            
        }];
        
    }
}

@end
